/* 微信接单列表空页面为 pages/empty/empty */
var httpUtils = require('../../utils/httpUtils.js');
var utils = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    currentTab: 0,
    clientHeight: 'auto',
    authHidden: false,
    wxList: [],
    toCompletedList: [],
    completedList: []
  },
  onLoad: function() {
    var res = wx.getSystemInfoSync();
    var clientHeight = ((res.windowHeight * 2)-234)+ 'rpx';
    var _this = this;
    this.setData({
      clientHeight: clientHeight
    });

    if (!utils.isNull(wx.getStorageSync('userInfo'))) { //如果都已经拿到号码了,那就不显示弹框了
      this.setData({
        authHidden: true
      });
    }

    if (!utils.isNull(wx.getStorageSync('userInfo'))) {
      var url = app.globalData.interfaceUrl + '/order.do?method=wxOrdeList';
      console.log(url);
      httpUtils.post(url, {
        'listType': 'dqd'
      }, function(res) {
        console.log(res);
        var wxOrderList = res.data.data;
        console.log(wxOrderList);
        _this.setData({
          wxList: wxOrderList
        });
      }, function(res) {
        console.log(res);
      });
    }
  },
  //获取用户手机号码
  getPhoneNumber: function(e) {
    var _this = this;
    var params = {};
    params.encryptedData = e.detail.encryptedData;
    params.iv = e.detail.iv;
    params.loginSessionId = wx.getStorageSync('loginSessionId');
    httpUtils.post(app.globalData.interfaceUrl + '/sysUser/auth.do?method=' + encodeURIComponent('getWorkerInfo'), params, function(res) {
      wx.setStorageSync('userInfo', res.data.data);
      _this.setData({
        authHidden: true
      });
      console.log(res);
      //拿到了手机号,再去到列表
      // wx.switchTab({
      // url: '../takeOrderList/takeOrderList'
      // });
      _this.onLoad();
    });
  },
  stopTouchMove: function () {
    return false;
  },
  clickTab: function(e) {
    var res = wx.getSystemInfoSync();
    var clientHeight = res.windowHeight * 2 + 'rpx';
    var _this = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current,
        clientHeight: clientHeight
      })
    }

    if ("0" == e.currentTarget.dataset.current) { //点了待施工列表
      var url = app.globalData.interfaceUrl + '/order.do?method=wxOrdeList';
      console.log(url);
      httpUtils.post(url, {
        'listType': 'dqd'
      }, function(res) {
        console.log(res);
        var wxOrderList = res.data.data;
        console.log(wxOrderList);
        _this.setData({
          wxList: wxOrderList
        });
      }, function(res) {
        console.log(res);
      });
    }
    if ("1" == e.currentTarget.dataset.current) { //点了待施工列表
      var url = app.globalData.interfaceUrl + '/order.do?method=recOrderList';
      console.log(url);
      httpUtils.post(url, {}, function(res) {
        console.log(res);
        var toCompletedList = res.data.data;
        console.log(toCompletedList);
        _this.setData({
          toCompletedList: toCompletedList
        });
      }, function(res) {
        console.log(res);
      });
    }

    if ("2" == e.currentTarget.dataset.current) { //点了已完工列表
      var url = app.globalData.interfaceUrl + '/order.do?method=completeOrderList';
      console.log(url);
      httpUtils.post(url, {}, function(res) {
        console.log(res);
        var completedList = res.data.data;
        console.log(completedList);
        _this.setData({
          completedList: completedList
        });
      }, function(res) {
        console.log(res);
      });
    }
  },
  gotoExpiredOrderList: function() {
    wx.navigateTo({
      url: '../expiredOrder/expiredOrder'
    })
  },
  gotoApplyforOrder: function(e) {
    var orderId = e.currentTarget.dataset.order_id;
    var wxOrderPrice = e.currentTarget.dataset.wx_order_price;
    var invite_user_id = e.currentTarget.dataset.invite_user_id ;
    wx.navigateTo({
      url: '../applyForOrder/applyForOrder?orderId=' + orderId + '&wxOrderPrice=' + wxOrderPrice + '&inviteUserId=' + invite_user_id
    })
  },
  goToConstruct: function(e) {
    var orderId = e.currentTarget.dataset.order_id;
    wx.navigateTo({
      url: '../orderInPrograss/orderInPrograss?order_id=' + orderId
    })
  },
  previewPic: function(e) {
    var index = e.currentTarget.dataset.index;
    var url = e.currentTarget.dataset.url ;
    utils.previewImg(index,[url]);
  }
})